interface ITreeNode{
    pid?:string|number
    id:string|number
    name:string
    children?:ITreeNode[]
}

export const Reg={
    email:/^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-z]{2,}$/,
    url:/https?:\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/,
    mobile:/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/,
    chinese:/^[\u0391-\uFFE5]+$/
}
export function uuid():string{
    // return Math.random().toString().substr(2)+"_"+(seq++)
    return Math.random().toString().substr(2)
}
// export function noop():void{1}
export function makeTree(list:ITreeNode[]):ITreeNode[]{
    const root:ITreeNode[] =  []
    const map:{[k:string]:ITreeNode,[k:number]:ITreeNode} = {}
    list.forEach((item:ITreeNode)=>{
        map[item.id]=item
    })
    list.forEach((item:ITreeNode)=>{
        if(item.pid!==0&&!item.pid){
            root.push(item)
        }else{
            map[item.pid].children = map[item.pid].children||[]
            map[item.pid].children?.push(item)
        }
    })
    return root
}
export function deep(list:ITreeNode[],fn:(item:ITreeNode)=>void):void{
    list.forEach((o:ITreeNode)=>{
        fn(o)
        o.children&&deep(o.children,fn)
    })
}
export function isEmpty(v:unknown){
    return v===undefined||v===""||v===null||(v instanceof Array&&v.join("")==="")
}
export function isUndef(a:unknown):boolean{
    return a===undefined||a===null
}
export function format(obj:Record<string,unknown>,format:string):string{
    return format.replace(/\$\{(\w+)\}/g,(a,b)=>obj[b]+"")
}
export function isNumber(a:unknown):boolean{
    return a!==undefined&&
            a!==''&&
            a!==null&&
            (typeof a !== "boolean")&&
            (typeof a !== "object")&&
            !isNaN(a as number)
}
export function numberRangeValid(start?:number,end?:number,min?:number,max?:number):string|boolean{
    const 
        s = start as number,
        e = end as number,
        mi = min as number,
        ma = max as number;
    if(isNumber(mi)&&isNumber(ma)){
        if(
            isNumber(s)&&(s<mi||s>ma)||
            isNumber(e)&&(e<mi||e>ma)
        )
            return `请录入${mi}-${ma}范围的数字`
    }
    else if(isNumber(mi)){
        if(
            (isNumber(s)&&s<mi)||
            (isNumber(e)&&e<mi)
        )
            return `请录入大于等于${mi}的数字`
    }
    else if(isNumber(ma)){
        if(
            (isNumber(s)&&ma<s)||
            (isNumber(e)&&ma<e)
        )
            return `请录入小于等于${ma}的数字`
    }
    if(!isNumber(s))
        return "请录入开始数字"
    if(!isNumber(e))
        return "请录入结束数字"
    if(e<s)
        return "开始数字不能大于结束数字"
    return true
}
